package cn.iocoder.yudao.module.wine.service.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.module.wine.controller.admin.config.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.config.WineConfigDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.wine.dal.mysql.config.WineConfigMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wine.enums.ErrorCodeConstants.*;

/**
 * 平台配置 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class WineConfigServiceImpl implements WineConfigService {

    @Resource
    private WineConfigMapper wineConfigMapper;

    @Override
    public Integer createConfig(ConfigSaveReqVO createReqVO) {
        // 插入
        WineConfigDO config = BeanUtils.toBean(createReqVO, WineConfigDO.class);
        wineConfigMapper.insert(config);
        // 返回
        return config.getId();
    }

    @Override
    public void updateConfig(ConfigSaveReqVO updateReqVO) {
        // 校验存在
        validateConfigExists(updateReqVO.getId());
        // 更新
        WineConfigDO updateObj = BeanUtils.toBean(updateReqVO, WineConfigDO.class);
        wineConfigMapper.updateById(updateObj);
    }

    @Override
    public void deleteConfig(Integer id) {
        // 校验存在
        validateConfigExists(id);
        // 删除
        wineConfigMapper.deleteById(id);
    }

    private void validateConfigExists(Integer id) {
        if (wineConfigMapper.selectById(id) == null) {
            throw exception(INFO_NOT_EXISTS);
        }
    }

    @Override
    public WineConfigDO getConfig(Integer id) {
        return wineConfigMapper.selectById(id);
    }

    @Override
    public PageResult<WineConfigDO> getConfigPage(ConfigPageReqVO pageReqVO) {
        return wineConfigMapper.selectPage(pageReqVO);
    }

    @Override
    public WineConfigDO getOne() {
        return wineConfigMapper.selectOne(new QueryWrapper<>());

    }

}
